package com.bohua.intermediary.service.impl;

import com.bohua.intermediary.common.Lay;
import com.bohua.intermediary.common.Msg;
import com.bohua.intermediary.dao.SysDeptMapper;
import com.bohua.intermediary.entity.SysDept;
import com.bohua.intermediary.service.SysDeptService;
import com.bohua.intermediary.vo.DeptVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务实现
 * @author qian
 * @email 1415261159@qq.com
 * @date 2019/01/19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDeptServiceImpl implements SysDeptService {
    @Resource
    private SysDeptMapper sysDeptMapper;

    @Override
    public Msg addSysDept(SysDept sysDept) {
        int i=  sysDeptMapper.insert(sysDept);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("添加成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("添加失败");
        }
        return msg;
    }

    @Override
    public Msg updateSysDept(SysDept sysDept) {
        int i= sysDeptMapper.updateByParam(sysDept);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("修改成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("修改失败");
        }
        return msg;
    }

    @Override
    public Msg delSysDept(SysDept sysDept) {
        int i=sysDeptMapper.del(sysDept.getId());
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("删除成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @Override
    public Lay getSysDepts(SysDept sysDept, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<SysDept> sysDepts=sysDeptMapper.getList(sysDept);
        PageInfo info=new PageInfo(sysDepts);
        Lay lay=new Lay();
        lay.setPage(page);
        lay.setCount(info.getTotal());
        lay.setData(info.getList());
        return lay;
    }


    @Override
    public DeptVo getList(SysDept sysDept) {
        List<SysDept> sysDepts=sysDeptMapper.getList(sysDept);
        DeptVo deptVo=new DeptVo();
        for (SysDept item:
             sysDepts) {
            /**
             *
             */
            if (item.getParentId()==0){
                deptVo.setId(item.getId());
                deptVo.setNaem(item.getDictName());
                deptVo.setChecked(true);
                deptVo.setOpen(true);
            }else {
                List<DeptVo> zhi=new ArrayList<>();
            }
        }
        return null;
    }
}
